package com.infoauto.opc.gateway.opc.utils;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;

//读取OPC数据类型转换成String工具类
public class DataTypeConversionUtils {

    public static  String DataTypeConversionToString(Variant variant){
        Object val = variant==null?"":variant.getValue();
        StringBuilder ret = new StringBuilder();
        if(val instanceof Boolean){
            ret.append((Boolean) val);
        } else if (val instanceof Short){
            ret.append((Short) val);
        } else if (val instanceof Integer){
            ret.append((Integer) val);
        } else if (val instanceof Long){
            ret.append((Long) val);
        } else if(val instanceof Float){
            ret.append((Float) val);
        } else if(val instanceof Double){
            ret.append((Double) val);
        } else if (val instanceof Byte){
            ret.append("\"");
            ret.append((Byte) val);
            ret.append("\"");
        } else if (val instanceof String){
            ret.append("\"");
            ret.append(val);
            ret.append("\"");
        } else if (val instanceof Boolean[]) {
            ret.append("[");
            ret.append(StringUtils.join((Boolean[]) val,","));
            ret.append("]");
        } else if (val instanceof Short[]) {
            ret.append("[");
            ret.append(StringUtils.join((Short[]) val,","));
            ret.append("]");
        } else if (val instanceof Integer[]) {
            ret.append("[");
            ret.append(StringUtils.join((Integer[]) val,","));
            ret.append("]");
        } else if (val instanceof Long[]) {
            ret.append("[");
            ret.append(StringUtils.join((Long[]) val,","));
            ret.append("]");
        } else if (val instanceof Float[]) {
            ret.append("[");
            ret.append(StringUtils.join((Float[]) val,","));
            ret.append("]");
        } else if (val instanceof Byte[]) {
            ret.append("[");
            for (Byte b : (Byte[])val){
                ret.append("\"");
                ret.append(b);
                ret.append("\",");
            }
            ret = ret.deleteCharAt(ret.length()-1);
            ret.append("]");
        } else if (val instanceof Double[]){
            ret.append("[");
            ret.append(StringUtils.join((Float[]) val,","));
            ret.append("]");
        } else {
            ret.append("\"");
            ret.append(val==null?"":val);
            ret.append("\"");
        }
        return ret.toString();
    }
    public static  Variant DataTypeConversion(Variant variant,String value){
        try{
            Object val = variant==null?"":variant.getValue();
            if(val instanceof Boolean){
                variant = new Variant(new Boolean(value));
            } else if (val instanceof Short){
                variant = new Variant(new Short(value));
            } else if (val instanceof Integer){
                variant = new Variant(new Integer(value));
            } else if (val instanceof Long){
                variant = new Variant(new Long(value));
            } else if(val instanceof Float){
                variant = new Variant(new Float(value));
            } else if(val instanceof Double){
                variant = new Variant(new Double(value));
            } else if (val instanceof Byte){
                variant = new Variant(new Byte(value));
            } else if (val instanceof String){
                variant = new Variant(value);
            } else if (val instanceof Boolean[]) {
                variant = new Variant(str2BooleanArray(value));
            } else if (val instanceof Short[]) {
                variant = new Variant(str2ShortArray(value));
            } else if (val instanceof Integer[]) {
                variant = new Variant(str2IntegerArray(value));
            } else if (val instanceof Long[]) {
                variant = new Variant(str2LongArray(value));
            } else if (val instanceof Float[]) {
                variant = new Variant(str2FloatArray(value));
            } else if (val instanceof Double[]){
                variant = new Variant(str2DoubleArray(value));
            } else {
                variant = new Variant(value==null?"":val);
            }
            return variant;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转Boolean数组
     * */
    public static Boolean[] str2BooleanArray(String str){
        String[] strArr = str.split(",");
        Boolean[] rst = null;
        if(strArr!=null) {
            rst = new Boolean[strArr.length];
            for(int i = 0; i < rst.length; i++) {
                rst[i] = new Boolean(strArr[i]);
            }
        }
        return rst;
    }

    /**
     * 字符串转Short数组
     * */
    public static Short[] str2ShortArray(String str){
        String[] strArr = str.split(",");
        Short[] rst = null;
        if(strArr!=null) {
            rst = new Short[strArr.length];
            for(int i = 0; i < rst.length; i++) {
                rst[i] = new Short(strArr[i]);
            }
        }
        return rst;
    }

    /**
     * 字符串转Integer数组
     * */
    public static Integer[] str2IntegerArray(String str){
        String[] strArr = str.split(",");
        Integer[] rst = null;
        if(strArr!=null) {
            rst = new Integer[strArr.length];
            for(int i = 0; i < rst.length; i++) {
                rst[i] = new Integer(strArr[i]);
            }
        }
        return rst;
    }

    /**
     * 字符串转Long数组
     * */
    public static Long[] str2LongArray(String str){
        String[] strArr = str.split(",");
        Long[] rst = null;
        if(strArr!=null) {
            rst = new Long[strArr.length];
            for(int i = 0; i < rst.length; i++) {
                rst[i] = new Long(strArr[i]);
            }
        }
        return rst;
    }

    /**
     * 字符串转Float数组
     * */
    public static Float[] str2FloatArray(String str){
        String[] strArr = str.split(",");
        Float[] rst = null;
        if(strArr!=null) {
            rst = new Float[strArr.length];
            for(int i = 0; i < rst.length; i++) {
                rst[i] = new Float(strArr[i]);
            }
        }
        return rst;
    }

    /**
     * 字符串转Double数组
     * */
    public static Double[] str2DoubleArray(String str){
        String[] strArr = str.split(",");
        Double[] rst = null;
        if(strArr!=null) {
            rst = new Double[strArr.length];
            for(int i = 0; i < rst.length; i++) {
                rst[i] = new Double(strArr[i]);
            }
        }
        return rst;
    }

}
